package com.docparser.processor;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 处理下标元素。
 */
public class SubscriptProcessor extends BaseProcessor {
    /**
     * 处理下标元素。
     */
    public static void process(Element subElement, StringBuilder formula, ProcessorCallback callback) {
        if (subElement == null) return;
        
        StringBuilder base = new StringBuilder();
        StringBuilder subscript = new StringBuilder();
        
        // 记录处理状态
        ProcessingState state = new ProcessingState();
        
        processChildren(subElement, base, subscript, state, callback);
        
        // 处理基础文本
        String baseText = base.toString().trim();
        String subText = subscript.toString().trim();
        
        appendFormula(formula, baseText, subText, state);
    }
    
    private static class ProcessingState {
        boolean hasContent = false;
        boolean hasNestedScripts = false;
        boolean needsBrackets = false;
    }
    
    private static void processChildren(Element element, StringBuilder base, StringBuilder subscript,
                                     ProcessingState state, ProcessorCallback callback) {
        NodeList children = element.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ELEMENT_NODE) continue;
            
            Element childElement = (Element) child;
            String nodeName = childElement.getNodeName();
            
            if ("m:e".equals(nodeName)) {
                if (base.length() == 0) {
                    callback.processElement(childElement, base);
                    // 检查基础文本中是否包含上下标
                    if (base.toString().contains("^") || base.toString().contains("_")) {
                        state.hasNestedScripts = true;
                    }
                } else {
                    callback.processElement(childElement, subscript);
                    state.hasContent = true;
                    // 检查下标文本中是否包含复杂表达式
                    String subContent = subscript.toString();
                    if (subContent.contains("+") || subContent.contains("-") ||
                        subContent.contains("\\frac") || subContent.contains("\\sqrt")) {
                        state.needsBrackets = true;
                    }
                }
            }
        }
    }
    
    private static void appendFormula(StringBuilder formula, String baseText, String subText, ProcessingState state) {
        if (baseText.isEmpty()) return;
        
        // 如果基础文本中有嵌套的上下标，需要用括号括起来
        if (state.hasNestedScripts) {
            formula.append("(").append(baseText).append(")");
        } else {
            formula.append(baseText);
        }
        
        if (state.hasContent) {
            if (state.needsBrackets || subText.length() > 1) {
                formula.append("_{").append(subText).append("}");
            } else {
                formula.append("_").append(subText);
            }
        }
    }
}
